package models

import (
	"github.com/olivere/elastic/v7"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func InitMysql(mysqlFunc func(DB *gorm.DB) error) error {
	dsn := "root:root@tcp(127.0.0.1:3306)/demo?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	if err != nil {
		panic(err)
	}
	chone, _ := db.DB()
	defer chone.Close()
	return mysqlFunc(db)
}
func InitEs(esFunc func(EsClient *elastic.Client) (interface{}, error)) (interface{}, error) {
	client, err := elastic.NewClient(elastic.SetURL("http://127.0.0.1:9200"), elastic.SetSniff(false))
	if err != nil {
		panic(err)
	}
	defer client.Stop()
	return esFunc(client)
}
func Migrate() error {
	return InitMysql(func(DB *gorm.DB) error {
		return DB.AutoMigrate(&TextType{}, &Text{}, &TextBarrage{}, &JvBao{})
	})
}

// TextType todo 文章分类表
type TextType struct {
	Id    int32  `gorm:"primaryKey"`
	Title string `gorm:"type:varchar(20);not null;comment:文章类型"`
}

// TextBarrage todo 评论表
type TextBarrage struct {
	gorm.Model
	TextId  int32  `gorm:"type:int(11);not null;comment:文章id"`
	Userid  int32  `gorm:"type:int(11);not null;comment:用户id"`
	Comment string `gorm:"type:TEXT;not null;comment:评论内容"`
	Pid     int32  `gorm:"type:int(11);not null;comment:父级id"`
}

// Text todo 文章信息表
type Text struct {
	gorm.Model
	Userid    int32  `gorm:"type:int(11);not null;comment:用户id" json:"userid"`
	Title     string `gorm:"type:varchar(30);not null;comment:文章标题" json:"title"`
	Comment   string `gorm:"type:TEXT;not null;comment:评论内容" json:"comment"`
	TypeId    int    `gorm:"type:int(11);not null;comment:文章分类" json:"type_id"`
	Tag       string `gorm:"type:varchar(30);not null;comment:文章标签" json:"tag"`
	LookCount int    `gorm:"type:int(11);not null;comment:浏览量" json:"look_count"`
	Status    int    `gorm:"type:tinyint(2);check:status IN (0,1);not null;comment:状态" json:"status"`
}

// JvBao todo 举报
type JvBao struct {
	gorm.Model
	TextId  int32  `gorm:"type:int(11);not null;comment:文章id"`
	Userid  int32  `gorm:"type:int(11);not null;comment:用户id" json:"userid"`
	Comment string `gorm:"type:TEXT;not null;comment:评论内容" json:"comment"`
	Status  int    `gorm:"type:tinyint(2);check:status IN (0,1);not null;comment:状态" json:"status"`
}
